package com.lykj.modules.shop.mapper;

import com.lykj.modules.shop.entity.UserExtract;
import com.lykj.modules.shop.excel.UserExtractExcel;
import com.lykj.modules.shop.vo.UserExtractVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 提现表 Mapper 接口
 *
 * @author cxl
 * @since 2022-01-21
 */
public interface UserExtractMapper extends BaseMapper<UserExtract> {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param userExtract
	 * @return
	 */
	List<UserExtractVO> selectUserExtractPage(IPage page, UserExtractVO userExtract);

	@Select("<script>select e.*,u.account, u.uid,u.name as nickname from shsy_user_extract e LEFT JOIN lykj_user u on u.id=e.user_id where e.is_deleted = 0" +
		"<if test=\"userExtract.id!=null\">" +
		" and e.id = #{userExtract.id}" +
		"</if>" +
		"<if test=\"userExtract.tenantId!=null and userExtract.tenantId!=''\">" +
		" and e.tenant_id = #{userExtract.tenantId}" +
		"</if>" +
		"<if test=\"userExtract.userId!=null\">" +
		" and e.user_id = #{userExtract.userId}" +
		"</if>" +
		"<if test=\"userExtract.account!=null and userExtract.account!=''\">" +
		" and u.account = #{userExtract.account}" +
		"</if>" +
		"<if test=\"userExtract.uid!=null\">" +
		" and u.uid = #{userExtract.uid}" +
		"</if>" +
		"<if test=\"userExtract.nickname!=null and userExtract.nickname!=''\">" +
		" and u.name = #{userExtract.nickname}" +
		"</if>" +
		"<if test=\"userExtract.znUserId!=null and userExtract.znUserId!=''\">" +
		" and e.zn_user_id = #{userExtract.znUserId}" +
		"</if>" +
		"<if test=\"userExtract.status!=null\">" +
		" and e.status = #{userExtract.status}" +
		"</if>" +
		"<if test=\"userExtract.startDate!=null and userExtract.endDate!=null\">" +
		" and e.create_time BETWEEN CONCAT(#{userExtract.startDate},' 00:00:00') AND CONCAT(#{userExtract.endDate},' 23:59:59')" +
		"</if>" +
		"</script>")
	List<UserExtractExcel> export(@Param("userExtract") UserExtractVO userExtract);

	/**
	 * 获取累计分红总额
	 */
	@Select("select if(sum(e.amount) is null,0,sum(e.amount)) from shsy_user_extract e where e.`status`=1 and e.is_deleted=0")
	BigDecimal sumAmount();

	/**
	 * 获取累计分红总额
	 */
	@Select("select if(sum(e.amount) is null,0,sum(e.amount)) from shsy_user_extract e where e.`status`=1 and user_id=#{userAccount} and e.is_deleted=0")
	BigDecimal sumUserAmount(@Param("userAccount") Long userAccount);
}
